Running Functions from the Command Line

We should be good with running print statements now and seeing the results on the command line.

Practically, same thing goes for running functions in the command line. What I mean is that for running print statements is simply a line of code.

For running a function on the command line:

  1. We have to define the function
  2. Inside the function write your code/logic
  3. Make a call to the function

Example


In [1]:
def hello():
    print "Hello World"

if I should run the code above i would not get any result because i am not making a call to the function... let's make that call


In [2]:
hello()


Hello World

As you can see until i made a call to the hello function above before it gave me a result.... This is the same understanding we would apply to the Script we are about to create now

Hands On

Create a file called script_function.py

In the File:

  1. Define a function called hello
  2. Inside the function write a print function that prints out "Hello World"
  3. Outside function call the function like this hello()
  4. Open the command line and navigate to the directory where you have your file
  5. Run the script the same way you ran the print statement script

And you should have the Hello world printed to the console

Practice

  1. Define 5 functions call them any name you want
  2. Inside the function write a print function that prints out different things
  3. Outside function call all the functions that you declared
  4. Open the command line and navigate to the directory where you have your file
  5. Run the script the same way you ran the print statement script

And then you should have 5 stuffs printed to the console

Disclaimer

if you dont understand how to declare functions....... you should go and read about it For any other problem on running the script.... Pls contact the group

Next

Receiving Arguments from the command Line